﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class D_Tutor
    {
        public int Add(List<Tutor> tutors)
        {
            var res = -1;
            try
            {
                string sqlteacher = "if not exists (select 1 from Tutor where teacher_id = @teacher_id and major_id=@major_id and grade=@grade)" +
                            "insert into Tutor(teacher_id, major_id, grade, major_name) values( @teacher_id, @major_id, @grade,@major_name)";
                             
                SqlParameter[] sps = new SqlParameter[4];

                foreach (var tutor in tutors)
                {
                    sps[0] = new SqlParameter("@teacher_id", tutor.teacher_id);
                    sps[1] = new SqlParameter("@major_id", tutor.major_id);
                    sps[2] = new SqlParameter("@grade", tutor.grade);
                    sps[3] = new SqlParameter("@major_name", tutor.major_name);
                    SqlDbHelper.ExecuteNonQuery(sqlteacher, System.Data.CommandType.Text, sps);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return res;
        }
    }
}
